﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace UnitTests
{
	public static class TestRegistry
	{
		private static Queue<TestCaseBase> testsToRun = new Queue<TestCaseBase>();
		private static Queue<TestCaseBase> uiThreadTests = new Queue<TestCaseBase>();
		private static int totalTests = 0;

		static TestRegistry()
		{
			Assembly thisAssembly = typeof(TestRegistry).Assembly;
			Type[] allTypes = thisAssembly.GetTypes();
			foreach (Type t in allTypes)
			{
				if (typeof(TestCaseBase).IsAssignableFrom(t) && !typeof(TestCaseBase).Equals(t))
				{
					totalTests++;

					TestCaseBase testCase = (TestCaseBase)thisAssembly.CreateInstance(t.FullName);
					object[] attributes = t.GetCustomAttributes(typeof(UiThreadAttribute), true);
					if (attributes.Length > 0)
					{
						uiThreadTests.Enqueue(testCase);
					}
					else
					{
						testsToRun.Enqueue(testCase);
					}
				}
			}
		}

		public static TestCaseBase PopTestCase(bool getUiThreadTest)
		{
			Queue<TestCaseBase> queue = getUiThreadTest
				? uiThreadTests
				: testsToRun;

			return queue.Count == 0
				? null
				: queue.Dequeue();
		}

		public static int TotalTests
		{
			get
			{
				return totalTests;
			}
		}
	}
}
